package com.keste.ojt1.view.task10.mkarumuri.bean;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.el.ExpressionFactory;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.input.RichInputFile;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import org.apache.myfaces.trinidad.model.UploadedFile;

public class FileUploadBean {
    private RichInputFile fileProcessorUpload;
  private BindingContainer bindings=null;
    public FileUploadBean() {
    }

    public void setFileProcessorUpload(RichInputFile fileProcessorUpload) {
        this.fileProcessorUpload = fileProcessorUpload;
    }

    public RichInputFile getFileProcessorUpload() {
        return fileProcessorUpload;
    }

    public void fileUpload(ValueChangeEvent valueChangeEvent) {
                // Add event code here...
                UploadedFile upFile = (UploadedFile)valueChangeEvent.getNewValue();
                       
                        List list=new ArrayList();
                             
                             try{
                               BufferedReader reader =
                                               new BufferedReader(new InputStreamReader(upFile.getInputStream()));  
                                 
                                 for(int i=0; i<7; i++){
                               String line = reader.readLine();
                                 StringTokenizer stok = new StringTokenizer(line, ",");
                                     List list1=new ArrayList();
                                 while(stok.hasMoreTokens()){
                                  
                                  list1.add(stok.nextToken());
                                 }       
                                     list.add(list1);
                              }
                               System.out.println("Token2: " +list);
                               BindingContainer bindings = getBindings();
                                       OperationBinding op = bindings.getOperationBinding("setCSVDatatoVO");
                                 op.getParamsMap().put("list", list);
                                         if (op != null) {
                                                            op.execute();
                                                        }
                                              }catch(Exception e){
                                                System.out.println("Trace: "+ e);
                                              }       
            }

       public void setBindings(BindingContainer bindings) {
        this.bindings = bindings;
    }
  /**
   * generic method to get handle to DCBindingContainer.
   * @return DCBindingContainer handle
   */
  public DCBindingContainer getDCBindingContainer() {
     
      FacesContext fctx = FacesContext.getCurrentInstance();
      ExpressionFactory expFactory =
          FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
      DCBindingContainer bc =
          
            (DCBindingContainer)expFactory.createValueExpression(fctx.getELContext(),
                                                               "#{bindings}",
                                                               DCBindingContainer.class).getValue(fctx.getELContext());

      return bc;
  }
  /**
       * This method returns the instance of BindingContainer.
       * @return BindingContainer.
       */
    public BindingContainer getBindings() {
      if (this.bindings == null) {
          this.bindings = (BindingContainer)getDCBindingContainer();
      }
        return bindings;
    }
  public void validateRows(ActionEvent actionEvent) {
      // Add event code here...
      BindingContainer bindings = getBindings();
                    OperationBinding op = bindings.getOperationBinding("setDataValidationVO");
            op.execute();
  }
}
